package com.adtech.farmProducts.adapter;

import android.icu.util.Calendar;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.AppCompatImageView;
import android.view.View;
import android.widget.TextView;

import com.adtech.common.Common;
import com.adtech.common.widgets.recycler.RecyclerViewAdapter;
import com.adtech.farmProducts.R;
import com.adtech.farmProducts.model.SourceInfo;
import com.adtech.farmProducts.utils.Constants;
import com.bumptech.glide.Glide;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;


/**
 * Author:  xiaocz.
 * Date:    2018/05.04.
 * Descr:  产品-时间轴
 */
public class SourceInfoAdapter extends RecyclerViewAdapter<SourceInfo> {

    public SourceInfoAdapter(AdapterListener<SourceInfo> adapterListener) {
        super(adapterListener);
    }

    @Override
    protected int getItemViewType(int position, SourceInfo data) {
        return R.layout.item_source_info;
    }

    @Override
    protected ViewHolder<SourceInfo> getViewHolder(View view, int viewType) {
        return new InfoTypeViewHolder(view);
    }

    private static class InfoTypeViewHolder extends ViewHolder<SourceInfo> {

        private TextView tv_one_line,tv_time;
        private TextView tv_one, tv_six, tv_two, tv_three, tv_four, tv_five;
        private TextView tv_c_one, tv_c_six, tv_c_two, tv_c_three, tv_c_four, tv_c_five;
        private AppCompatImageView iv_image;

        public InfoTypeViewHolder(View itemView) {
            super(itemView);
            tv_one_line=itemView.findViewById(R.id.tv_one_line);
            tv_time = itemView.findViewById(R.id.tv_time);
            tv_one = itemView.findViewById(R.id.tv_one);
            tv_two = itemView.findViewById(R.id.tv_two);
            tv_three = itemView.findViewById(R.id.tv_three);
            tv_four = itemView.findViewById(R.id.tv_four);
            tv_five = itemView.findViewById(R.id.tv_five);
            tv_six = itemView.findViewById(R.id.tv_six);
            tv_c_one = itemView.findViewById(R.id.tv_c_one);
            tv_c_two = itemView.findViewById(R.id.tv_c_two);
            tv_c_three = itemView.findViewById(R.id.tv_c_three);
            tv_c_four = itemView.findViewById(R.id.tv_c_four);
            tv_c_five = itemView.findViewById(R.id.tv_c_five);
            tv_c_six = itemView.findViewById(R.id.tv_c_six);
            iv_image=itemView.findViewById(R.id.iv_image);
        }

        @Override
        protected void onBind(SourceInfo data, int position) {
            if(position==0){
                tv_one_line.setBackgroundColor(ContextCompat.getColor(mContext,R.color.white));
            }else {
                tv_one_line.setBackgroundColor(ContextCompat.getColor(mContext,R.color.btn_color));
            }
            SimpleDateFormat formatter = new SimpleDateFormat(Common.DateType.dateIF, Locale.CHINA);
            try {
                Date date = formatter.parse(data.getHandleTime());
                Calendar cal = Calendar.getInstance();
                cal.setTime(date);
                tv_time.setText(data.getHandleTime());
            } catch (ParseException e) {
                e.printStackTrace();
            }
            tv_one.setText("类型");
            if(data.getImageUrl()!=null && !"".equals(data.getImageUrl())){
                iv_image.setVisibility(View.VISIBLE);
                Glide.with(mContext)
                        .load(data.getImageUrl())
                        .into(iv_image);
            }else {
                iv_image.setVisibility(View.GONE);
            }
            switch (data.getHandleType()) {
                case 1://1、入库
                    tv_c_one.setText("装箱赋码");
                    tv_two.setText("重量");
                    tv_three.setText("包装单位");
                    tv_four.setText("装箱人");
                    tv_five.setText("装箱公司");
                    tv_six.setText("装箱地址");
                    tv_c_two.setText(String.format(data.getWeight() + "%s", data.getUnit()));
                    tv_c_three.setText(data.getPackUnit());
                    tv_c_four.setText(data.getUserName());
                    tv_c_five.setText(data.getCompanyName());
                    tv_c_six.setText(data.getAddress());
                    break;
                case 2://2、开箱验货
                    tv_c_one.setText("开箱验货");
                    tv_two.setText("验货地址");
                    tv_three.setText("验货单位");
                    tv_four.setText("验货人");
                    tv_five.setText("旧溯源码");
                    tv_six.setText("新溯源码");
                    tv_c_two.setText(data.getAddress());
                    tv_c_three.setText(data.getCompanyName());
                    tv_c_four.setText(data.getUserName());
                    tv_c_five.setText(data.getOldSourceCode());
                    tv_c_six.setText(data.getSourceCode());
                    break;
                case 3://3、标签破损
                    tv_c_one.setText("标签更换");
                    tv_two.setText("操作地址");
                    tv_three.setText("操作单位");
                    tv_four.setText("操作人");
                    tv_five.setText("旧溯源码");
                    tv_six.setText("新溯源码");
                    tv_c_two.setText(data.getAddress());
                    tv_c_three.setText(data.getCompanyName());
                    tv_c_four.setText(data.getUserName());
                    tv_c_five.setText(data.getOldSourceCode());
                    tv_c_six.setText(data.getSourceCode());
                    break;
                case 4://4、集中采购
                    tv_c_one.setText("集中采购");
                    tv_two.setText("采购地址");
                    tv_three.setText("采购单位");
                    tv_four.setText("采购人");
                    tv_five.setText("重量");
                    tv_six.setText("溯源码");
                    tv_c_two.setText(data.getAddress());
                    tv_c_three.setText(data.getCompanyName());
                    tv_c_four.setText(data.getUserName());
                    tv_c_five.setText(data.getQuantity());
                    tv_c_six.setText(data.getSourceCode());
                    break;
                case 5://5、扫码上架
                    tv_c_one.setText("扫码上架");
                    tv_two.setText("上架地址");
                    tv_three.setText("上架单位");
                    tv_four.setText("上架人");
                    tv_five.setText("批次号");
                    tv_six.setText("上架重量");
                    tv_c_two.setText(data.getAddress());
                    tv_c_three.setText(data.getCompanyName());
                    tv_c_four.setText(data.getUserName());
                    tv_c_six.setText(data.getQuantity());
                    break;
            }
        }

        @Override
        public boolean isNeedClick() {
            return true;
        }
    }
}